/***************************************************************************
 *   mgpi_mdl_test.c - MGPI MDL Model Loader Test	                   *
 *   Copyright (C) 2008 by cmaster.matso                                   *
 *   cmaster.matso@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/*
 * Based on the work of David HENRY
 * Copyright (c) 2005-2007
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <MGPI/mgpi.h>

mdl_model_t *mdlfile;

void init(const char *filename) {
    GLfloat lightpos[] = {5.0f, 10.0f, 0.0f, 1.0f};

    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glShadeModel(GL_SMOOTH);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

    mdlfile = (mdl_model_t *) malloc(sizeof (mdl_model_t));

    if (!gsubReadMDLModel(filename, mdlfile)) {
        exit(1);
    }
    return;
}

void shutdownApp(void) {

    if (mdlfile) {
        gsubFreeMDLModel(mdlfile);
        free(mdlfile);
        mdlfile = NULL;
    }
}

void reshape(int w, int h) {
    if (h == 0)
        h = 1;

    glViewport(0, 0, (GLsizei) w, (GLsizei) h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat) w / (GLfloat) h, 0.1, 1000.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display(void) {
    static int n = 0;
    static float interp = 0.0;
    static double curent_time = 0;
    static double last_time = 0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    last_time = curent_time;
    curent_time = (double) glutGet(GLUT_ELAPSED_TIME) / 1000.0;

    interp += 10 * (curent_time - last_time);
    gsubAnimateMDL(0, mdlfile->header.num_frames - 1, &n, &interp);

    glTranslatef(0.0f, 0.0f, -100.0f);
    glRotatef(-90.0f, 1.0, 0.0, 0.0);
    glRotatef(-90.0f, 0.0, 0.0, 1.0);

    gsubRenderMDLModel(n, mdlfile);

    glutSwapBuffers();
    glutPostRedisplay();
    return;
}

void keyboard(unsigned char key, int x, int y) {
    switch (key) {
        case '+':
            mdlfile->iskin++;
            break;

        case '-':
            mdlfile->iskin--;
            break;

        case 27:
            exit(0);
            break;
    }
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "usage: %s filename.mdl\n", argv[0]);
        return 0;
    }

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(640, 480);
    glutCreateWindow(argv[1]);

    atexit(shutdownApp);
    init(argv[1]);

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();

    return 0;
}
